﻿// JScript 文件

Ext.onReady(function () {
    // 使用表单提示
    Ext.QuickTips.init();
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var sm = new Ext.grid.CheckboxSelectionModel();

    var cm = new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(),
		sm,
		{ header: '登陆名', width: 110, align: 'center', dataIndex: 'cusLoginName' },
		{ header: '姓名', width: 90, align: 'center', dataIndex: 'cusName' },
		{ header: '性别', width: 100, align: 'center', dataIndex: 'cusSex' },
        { header: 'Email', width: 200, align: 'center', dataIndex: 'cusEmail' },
        { header: '电话', width: 90, align: 'center', dataIndex: 'cusPhone' },
        { header: 'QQ', width: 100, align: 'center', dataIndex: 'cusQQ' },
        { header: '省', width:100, align:'center',dataIndex:'cusProvince'},
        { header: '市', width: 100, align: 'center', dataIndex: 'cusCity' },
        { header: '状态', width: 70, align: 'center', dataIndex: 'cusType', renderer: function (v) {
            if (v == 0) {
                return "正常";
            } else if (v == 1) {
                return "禁用";
            }
        }
        }

	]);

    cm.defaultSortable = true;

    var ds = new Ext.data.GroupingStore({
        proxy: new Ext.data.HttpProxy({
            url: 'ashx/Custom_GetByPage.ashx'
        }),
        reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
        },
		[{ name: 'id', mapping: 'cusID', type: 'string' },
         { name: 'cusLoginName',  type: 'string' },
		 { name: 'cusName', type: 'string' },
         { name: 'cusEmail', type: 'string' },
		 { name: 'cusType', type: 'string' },
		 { name: 'cusLoginTime', type: 'string' },
         { name: 'cusSex', type: 'string' },
         { name: 'cusProvince', type: 'string' },
         { name: 'cusCity', type: 'string' },
         { name: 'cusPhone', type:'string'},
         { name: 'cusQQ', type: 'string' } 
		])
    });
    var typeData = new Ext.data.SimpleStore( { fields : ["typeValue", "text"], data:[["-1","所有"],["0","正常使用"],["1","禁用"]] } );
    var searchbar = new Ext.Toolbar({
        id: 'searchbar',
        items: ['姓名  ： ',
		    new Ext.form.TextField({
		        id: 'realname',
		        width: 100
		    }), ' ',
		    ' 登陆名  ： ',
		    new Ext.form.TextField({
		        id: 'loginname',
		        width: 100
		    }), ' ',
		    ' 状态  ： ',
		    new Ext.form.ComboBox({
		        id: 'type',
		        width: 100,
		        store : typeData,
		        mode : "local",
		        editable : false,
		        valueField : "typeValue",
                displayField : "text",
                hiddenName : "cusType", 
                triggerAction: 'all' 		        
		    }), ' ',
		    
			new Ext.Toolbar.Button({
			    id: 'searchbutton',
			    iconCls: 'icon_search',
			    text: '查询',
			    handler: doSearch
			})
		]
    });

    function doSearch() {
        ds.load(
	        { params: {
	            start: 0,
	            limit: 20,
	            realname: Ext.get('realname').dom.value,
	            loginname:Ext.get('loginname').dom.value,
	            type:Ext.get('cusType').getValue()
	        }
	        });
    }

    var paging = new Ext.PagingToolbar({
        pageSize: 20,
        store: ds,
        displayInfo: true,
        displayMsg: '显示第{0}条到{1}条记录，一共{2}条',
        items:
		[
//		"-", " ",
//		    { text: '添加',
//		        tooltip: '添加一条系统用户信息',
//		        iconCls: 'icon-add',
//		        handler: function () {
//		            addNewUser();
//		        }
//		    },

		 "-", " ", { text: '禁用',
		     tooltip: '让客户禁用系统',
		     iconCls: 'icon-del',
		     handler: function () {

		         var _record = grid.getSelectionModel().getSelected();
		         if (sm.getCount() == 1) {
		             del_user(_record,1);
		         } else
		             Ext.Msg.alert('提示', '请选择一条记录！');
		         

		     }
		 },
	    "-", " ",
	    { text: '取消禁用',
	        tooltip: '修改一条系统用户信息',
	        iconCls: 'icon-edit',
	        handler: function () {
	            var _record = grid.getSelectionModel().getSelected();
	            if (sm.getCount() == 1) {
	                 del_user(_record,0);
	            } else
	                Ext.Msg.alert('提示', '请选择一条记录！');
	        }
	    }
		],
        emptyMsg: "查询记录为空!"
    });

    var grid = new Ext.grid.EditorGridPanel({
        renderTo: 'grid-div',
        cm: cm,
        ds: ds,
        sm: sm,
        tbar: searchbar,
        bbar: paging,
        height: 500,
        border: true,
        bodyStyle: 'width:1020px',
        stripeRows: true,
        title: '用户管理'
    });

    ds.load({ params: { start: 0, limit: 20} });

    function del_user(record,forb) {

        var myMask = new Ext.LoadMask(Ext.getBody(), {
            msg: "Please wait..."
        });
        Ext.Ajax.request({
            url: 'ashx/Custom_Del.ashx',
            params:{
                 cusId:record.data.id,
                 forbid:forb
            },
            success: function (response) {
                ds.load();
                myMask.hide();
            },
            failure: function () {

                Ext.Msg.alert('提示', '操作失败！');
                myMask.hide();
            }
        });
    }


    


});
 